import urllib2
import urllib
import simplejson as json


class WowCountry:
    UnitedStates = 'us'
    Europe = 'eu'


class WowQuery():

    _request_url = None

    def __init__(self, WowCountry):
        self._request_url = '%s%s%s' % ('http://', WowCountry,
                                                     '.battle.net/api/wow/')

    def _do_request(cls, full_request_url):
        request = urllib2.Request(full_request_url)
        json_data = urllib2.urlopen(request)
        data = json.load(json_data)
        return data

    def display_wow_query_data(self):
        print self._request_url

    def fetch_all_realm_data(self):
        realm_url = self._request_url + 'realm/status/'
        fetched_realms_data = self._do_request(realm_url)
        fetched_realms_data_collection = []
        for fetched_realm in fetched_realms_data['realms']:
            fetched_realms_data_collection.append(
            Realm(status=str(fetched_realm['status']),
            name=unicode(fetched_realm['name']),
            queue=fetched_realm['queue'],
            type=fetched_realm['type'],
            slug=fetched_realm['slug'],
            population=fetched_realm['population']))
        return fetched_realms_data_collection

    def fetch_specific_realm_data(self, realm_name):
        realm_url = self._request_url
        + 'realm/status?realms = %s' % (urllib.quote(realm_name))
        fetched_realms_data = self._do_request(realm_url)
        fetched_realm = Realm(status=str(
            fetched_realms_data['realms'][0]['status']),
            name=unicode(fetched_realms_data['realms'][0]['name']),
            queue=fetched_realms_data['realms'][0]['queue'],
            type=fetched_realms_data['realms'][0]['type'],
            slug=fetched_realms_data['realms'][0]['slug'],
            population=fetched_realms_data['realms'][0]['population'])
        return fetched_realm

    def fetch_guild_data(self, realm_name, guild_name):
        guild_url = self._request_url + 'guild/%s/%s' % (
            urllib.quote(realm_name), urllib.quote(guild_name))
        fetched_guild_data = self._do_request(GuildUrl)
        fetched_guild = Guild(lastModified=fetched_guild_data['lastModified'],
            name=fetched_guild_data['name'],
            realm=fetched_guild_data['realm'],
            level=fetched_guild_data['level'],
            side=fetched_guild_data['side'],
            achievementPoints=fetched_guild_data['achievementPoints'])
        return fetched_guild

    def fetch_all_guild_members(self, realm_name, guild_name):
        guild_members_url = self._request_url + 'guild/%s/%s?%s' % (
            urllib.quote(RealmName),
            urllib.quote(GuildName), 'fields = members')
        fetched_guild_members_data = self._do_request(GuildMembersUrl)
        fetched_guild_members_collection = []
        for fetched_guild_member in fetched_guild_members_data['members']:
            guild_member = Character(
                name=fetched_guild_member['character']['name'],
                memberClass=fetched_guild_member['character']['class'],
                race=fetched_guild_member['character']['name'],
                gender=fetched_guild_member['character']['gender'],
                realm=fetched_guild_member['character']['realm'],
                level=fetched_guild_member['character']['level'],
                achievementPoints=fetched_guild_member['character']['achievementPoints'],
                thumbnail=fetched_guild_member['character']['thumbnail'],
                rank=fetched_guild_member['rank'])
            fetched_guild_members_collection.append(guild_member)
        return fetched_guild_members_collection

    def fetch_character_data(self, realm_name, character_name):
        character_url = self._request_url + 'character/%s/%s' % \
        (urllib.quote(realm_name), urllib.quote(character_name))
        fetched_character_data = self._do_request(character_url)
        fetched_character = Character(name=fetched_character_data['name'],
            memberClass=fetched_character_data['class'],
            race=fetched_character_data['name'],
            gender=fetched_character_data['gender'],
            realm=fetched_character_data['realm'],
            level=fetched_character_data['level'],
            achievement_points=fetched_character_data['achievementPoints'],
            thumbnail=fetched_character_data['thumbnail'])
        return fetched_character

    def fetch_all_races(self):
        race_url = self._request_url + '/data/character/races'
        fetched_race_data = self._do_request(race_url)
        fecthed_race_collection = []
        for fetched_race in fetched_race_data['races']:
            race = Race(name=fetched_race['name'],
                id=fetched_race['id'],
                mask=fetched_race['mask'],
                side=fetched_race['side'])
            fecthed_race_collection.append(race)
        return fecthed_race_collection

    def fetch_all_classes(self):
        class_url = self._request_url + '/data/character/classes'
        fetched_class_data = self._do_request(ClassUrl)
        fetched_class_collection = []
        for fetched_class in fetched_class_data['classes']:
            class_ = Race(name=fetched_class['name'],
                id=fetched_class['id'],
                mask=fetched_class['mask'],
                powerType=fetched_class['powerType'])
            fetched_class_collection.append(class_)
        return fetched_class_collection
